package com.example.designpatterns.singleton;

/**
 * lazy loading
 * 最终版：线程安全 ，双重校验，效率下降
 * 个人感觉没必要。太麻烦
 */
public class Mgr04 {
    private static volatile Mgr04 INSTANCE;

    private Mgr04() {

    }

    public static Mgr04 getInstance() {
        if (INSTANCE == null) {
            //双重校验
            synchronized (Mgr04.class) {
                if (INSTANCE == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr04();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mgr04.getInstance().hashCode());
            }).start();
        }
    }
}
